package org.etom.impl.connector.datatypes;

import org.etom.connector.datatypes.IMoodleConnection;
import org.etom.connector.datatypes.IMoodleUser;
import org.etom.connector.service.ConnectorException;
import org.etom.impl.connector.util.Utility;
import org.etom.impl.connector.util.XmlRpcClientWrapper;

/**
 * The implementation of the Moodle connection.
 * The instances are immutable.
 */
public class MoodleConnectionImpl implements IMoodleConnection {
  
  private String moodleUrl;
  private String rpcServerUrl;
  private String username;
  private String password;
  private XmlRpcClientWrapper rpcClientWrapper;
//  private IMoodleUser moodleUser;
  
  /**
   * Constructs a new Moodle connection.
   * 
   * @param moodleUrl - the Moodle URL
   * @param rpcServerUrl - the path to the RPC server
   * @param username - the connection user name
   * @param password - the connection password
   * 
   * @throws ConnectorException if the URL is not valid.
   */
  public MoodleConnectionImpl(String moodleUrl,
      String rpcServerUrl, String username, String password) throws ConnectorException {
    this.moodleUrl = moodleUrl;
    this.rpcServerUrl = rpcServerUrl;
    this.username = username;
    this.password = password;
    init();
  }

  /**
   * @see org.etom.connector.datatypes.IMoodleConnection#getMoodleUrl()
   */
  public String getMoodleUrl() {
    return moodleUrl;
  }
  
  /**
   * @see org.etom.connector.datatypes.IMoodleConnection#getRpcServerUrl()
   */
  public String getRpcServerUrl() {
    return rpcServerUrl;
  }
  
  /**
   * @see org.etom.connector.datatypes.IMoodleConnection#getUserName()
   */
  public String getUserName() {
    return username;
  }
  
  /**
   * @see org.etom.connector.datatypes.IMoodleConnection#getPassword()
   */
  public String getPassword() {
    return password;
  }
  
  /**
   * @see org.etom.connector.datatypes.IMoodleConnection#getUser()
   */
  public IMoodleUser getUser() throws ConnectorException {
//    if (moodleUser == null) {
//      moodleUser = rpcClientWrapper.getMoodleUser(this,
//          Utility.getInitialProperties());
//    }
//    return moodleUser;
    return rpcClientWrapper.getMoodleUser(this, Utility.getInitialProperties());
  }
  
  @Override
  public String toString() {
    return username + '@' + moodleUrl + rpcServerUrl;
  }
  
  /*
   * An utility method which initializes the connection
   */
  private void init() throws ConnectorException {
    rpcClientWrapper = new XmlRpcClientWrapper(moodleUrl, rpcServerUrl);
  }

  @Override
  public boolean equals(Object other) {
    if (!(other instanceof IMoodleConnection)) {
      return false;
    }
    IMoodleConnection otherConnection = (IMoodleConnection) other;
    return this.moodleUrl.equals(otherConnection.getMoodleUrl()) &&
           this.rpcServerUrl.equals(otherConnection.getRpcServerUrl()) &&
           this.username.equals(otherConnection.getUserName()) &&
           this.password.equals(otherConnection.getPassword());
  }

  @Override
  public int hashCode() {
    return (this.moodleUrl.hashCode() << 8)    |
           (this.rpcServerUrl.hashCode() << 8) |
           (this.username.hashCode() << 8)     |
           (this.password.hashCode() << 8);
  }
  
}
